<?php

class LoginView {
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	
	/**
	 * String for input username.
	 * @var String
	 */
	private static $inputUsername = "UsernameID";
	
	/**
	 * String for input password.
	 * @var String
	 */
	private static $inputPassword = "PasswordID";
	
	 /** 
	 * String for checkbox "Save login data".
	 * @var String
	 */
	private static $saveLoginData = "RememberMe";
	
	/**
	 * @var String Cookie name for Username
	 */
	private static $cookieUsername = "CookieUsername";
	
	/**
	 * @var String Cookie name for Password
	 */
	private static $cookiePassword = "CookiePassword";
	
	/**
	 * URL string for when user wants to logout.
	 * @var String
	 */
	private static $logoutURL = "logout";
	
	/**
	 * URL string for when user wants to login.
	 * @var String
	 */
	private static $loginURL = "login";
	
	/**
	 * @var String
	 */
	private $loginMessage = "";
	
	/**
	 * @var String
	 */
	private $username = "";
	
	/**
	 * @var String
	 */
	private $password = "";
	
	
	/**
	 * @param NavigationView $navigationView
	 */
	public function __construct(NavigationView $navView) {
		$this->navView = $navView;
		
		if ($this->navView->userWasRegistered()) {
			$this->loginMessage = $this->createLoginMessage("User was registered.", false);
			$this->username = $this->navView->getUserWasRegisteredUsername();
		}
	}
								
	/**
	 * @return String HTML
	 */
	public function getLoginHTML() {
		return "<form action='?".$this->navView->getLoginURL()."' method='post'>
					<fieldset>
						<legend>Login - Type Username and Password</legend>
						$this->loginMessage
						<label for='".self::$inputUsername."'>Name:</label>
						<input type='text' name='".self::$inputUsername."' id='".self::$inputUsername."' value='$this->username' />
						<label for='".self::$inputPassword."'>Password:</label>
						<input type='password' name='".self::$inputPassword."' id='".self::$inputPassword."' />
						<label for='".self::$saveLoginData."'>Remember me:</label>
						<input type='checkbox' name='".self::$saveLoginData."' id='".self::$saveLoginData."'/>
						<input type='submit' value='Login'/>
					</fieldset>
				</form>";
	}
	
	/**
	 * @return String HTML
	 */
	public function getLogoutHTML () {
		return "<a href='?".$this->navView->getLogoutURL()."' id='LogoutButton'>Logout</a>
				$this->loginMessage";
	}
	
	public function createLoginMessage($str, $isErrorMessage) {
		if ($isErrorMessage) {
			return "<p class='ErrorMessage'>$str</p>";
		}
		else {
			return "<p class='SuccessMessage'>$str</p>";
		}
	}
	
	/**
	 * Messages when trying logging in user.
	 */
	public function userAuthed($authed) {
		if ($authed) {
			if ($this->saveLoginData()) {
				$this->loginMessage = $this->createLoginMessage("Login successful and we'll remember you next time", false);
			}
			else {
				if ($this->isCookiesSet()) {
					$this->loginMessage = $this->createLoginMessage("Login successful via cookies", false);
				}
				else {
					$this->loginMessage = $this->createLoginMessage("Login successful", false);
				}
			}
		}
		else {
			if ($this->isCookiesSet()) {
				$this->deleteLoginDataCookies();
				$this->loginMessage = $this->createLoginMessage("Incorrect information in the cookie", true);
			}
			else {
				$this->loginMessage = $this->createLoginMessage("Invalid username and/or password", true);
			}
		}
	}
	
	public function saveLoginData() {
		return isset($_POST[self::$saveLoginData]);
	}
	
	/**
	 * Logout the user.
	 */
	public function logoutUser() {
		$this->deleteLoginDataCookies();
		$this->loginMessage = $this->createLoginMessage("You have been logged out", false);
	}
	
	/**
	 * Set Cookies for username and encrypted password.
	 * @var Timestamp $endTime
	 */
	public function setLoginDataCookies($username, $password, $expireDate) {
		setcookie(self::$cookieUsername, $username, $expireDate);
		setcookie(self::$cookiePassword, $password, $expireDate);
	}
	
	private function deleteLoginDataCookies() {
		setcookie(self::$cookieUsername, "", time() -1);
		setcookie(self::$cookiePassword, "", time() -1);
	}
	
	public function isCookiesSet() {
		if(isset($_COOKIE[self::$cookieUsername]) && isset($_COOKIE[self::$cookiePassword])) {
		   	return true;
	    }
		return false;
	}
	
	/**
	 * Check if user input is not empty.
	 * @return Boolean
	 */
	public function isInputDataSet() {
		// Username check.
		if (!empty($_POST[self::$inputUsername])) {
			$this->username = strip_tags($_POST[self::$inputUsername]);
		}
		else {
			$this->loginMessage = $this->createLoginMessage("Username is missing", true);
			return false;
		}
		
		// Password check.
		if (!empty($_POST[self::$inputPassword])) {
			$this->password = strip_tags($_POST[self::$inputPassword]);
		}
		else {
			$this->loginMessage = $this->createLoginMessage("Password is missing", true);
			return false;
		}
		
		return true;
	}
	
	/**
	 * @return String
	 */
	public function getUsername() {
		if ($this->isCookiesSet()) {
			return $_COOKIE[self::$cookieUsername];
		}
		return $this->username;
	}
	
	/**
	 * @return String
	 */
	public function getPassword() {
		if ($this->isCookiesSet()) {
			return $_COOKIE[self::$cookiePassword];
		}
		return $this->password;
	}
}
